package com.boot01.controller;

import com.boot01.dao.DepartmentDao;
import com.boot01.dao.EmployeeDao;
import com.boot01.pojo.Department;
import com.boot01.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;

    /**
     * 查询客户列表
     */
    @RequestMapping("employeeList")
    public String list(Model model) {
        Collection<Employee> employeeList = employeeDao.getEmployees();
        model.addAttribute("employeeList", employeeList);
        return "employee/list";
    }

    /**
     * 跳转到添加客户页面
     */
    @GetMapping("employeeAdd")
    public String actionAddPage(Model model) {
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments", departments);
        return "employee/add";
    }

    /**
     * 添加客户
     */
    @PostMapping("employeeAdd")
    public String addEmployee(Employee employee) {
        employeeDao.addEmployee(employee);
        return "redirect:/employeeList";
    }

    /**
     * 跳转到更新客户页面
     */
    @GetMapping("/actionEmployeeUpdate")
    public String actionEmployeeUpdate(Integer employeeId, Model model) {
        System.out.println("id ====> "+employeeId);
        // 将选中的客户数据传递到Model
        Employee employee = employeeDao.getEmployeeById(employeeId);
        model.addAttribute("employee", employee);
        // 客户数据需要部门
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments", departments);
        return "employee/upload";
    }

    /**
     * 删除客户
     */
    @GetMapping("deleteEmployee/{id}")
    public String deleteEmployee(@PathVariable Integer id){
        employeeDao.deleteEmployee(id);
        return "redirect:/employeeList";
    }
}
